﻿using System;
using System.Collections.Generic;
using System.Configuration.Assemblies;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 绩效管理系统.Common
{
    public class FormFactory
    {
        /*private static FormUserManager formUserManager;
        private static FrmBaseManager frmBaseManager;
        private static FrmNone frmNone;*/
        private static Form form;
        private static List<Form> forms = new List<Form>();
        /*public static Form CreateForm(int index)
        {
            HideFormAll();
            switch (index)
            {
                
                case 0:
                    if (formUserManager == null)
                    {
                        formUserManager = new FormUserManager();
                        forms.Add(formUserManager);
                        //formUserManager.MdiParent = this;
                        //formUserManager.Parent = splitContainer1.Panel2;
                    }
                    form = formUserManager;
                    //frmBaseManager?.Hide();
                    //formUserManager.Show();
                    break;
                case 1:
                    if (frmBaseManager == null)
                    {
                        frmBaseManager = new FrmBaseManager();
                        forms.Add(frmBaseManager);
                        //frmBaseManager.MdiParent = this;
                        //frmBaseManager.Parent = splitContainer1.Panel2;
                    }
                    form = frmBaseManager;
                    //formUserManager?.Hide();
                    //frmBaseManager.Show();
                    break;
                default:
                    if (frmNone == null)
                    {
                        frmNone = new FrmNone();
                        forms.Add(frmNone);
                        //formUserManager.MdiParent = this;
                        //formUserManager.Parent = splitContainer1.Panel2;
                    }
                    form = frmNone;
                    //frmBaseManager?.Hide();
                    //formUserManager.Show();
                    break;
            }
            return form;
        
    }*/
        private static List<Type> types;
        static FormFactory()
        {
            Assembly ass = Assembly.LoadFrom("绩效管理系统.exe");
            types = ass.GetTypes().ToList();
        }
        
        public static Form CreateForm(string formName)
        {
            //string path = AppDomain.CurrentDomain.BaseDirectory;
            HideFormAll();
            formName = formName == null ? "FrmNone" : formName;
            Form form = forms.Find(m => m.Name == formName);
            
            if (form ==null)
            {
                Type type = types.Find(m => m.Name == formName);
                form = (Form)Activator.CreateInstance(type);
                forms.Add(form);
            }
            return form;
        }
        public static void HideFormAll()
        {
            foreach (var form in forms)
            {
                form.Hide();
            }
        }
    }
}
